﻿
using TianYu.Core.Pdf.ContentElement;

namespace TianYu.Core.Pdf.TemplateDocument.Line
{
    public class LineElement : AbstractElement
    {
        /// <summary> 线粗细 </summary>
        public float LineWidth { get; set; }

        /// <summary> 线样式 </summary>
        public int Style { get; set; }

        /// <summary> Wline＝水平线 Hline＝垂直线 </summary>
        public string LineType { get; set; }

        public override void Draw(PdfTemplateContent pdfContentByte)
        {
            if (Width == 0 || Height == 0 || LineWidth == 0) return;

            var lineStyle = CommonHelper.GetLineStyle(Style);

            pdfContentByte.PdfWriter.DirectContent.SetLineWidth(LineWidth / CommonHelper.LineWidthReduce);//细1.5倍是最接近后台设置的粗细
            pdfContentByte.PdfWriter.DirectContent.SetColorStroke(iTextSharp.text.BaseColor.BLACK);
            pdfContentByte.PdfWriter.DirectContent.MoveTo(X, Y);

            if (LineType == "Wline")
            {
                pdfContentByte.PdfWriter.DirectContent.LineTo(X + Width, Y);
            }
            else
            {
                pdfContentByte.PdfWriter.DirectContent.LineTo(X, Y + Height);
            }


            if (lineStyle == LineStyle.Dashed)
            {
                pdfContentByte.PdfWriter.DirectContent.SetLineDash(3, 3);
            }
            else if (lineStyle == LineStyle.Dotted)
            {
                pdfContentByte.PdfWriter.DirectContent.SetLineDash(1, 1);
            }

            pdfContentByte.PdfWriter.DirectContent.Stroke();
            pdfContentByte.PdfWriter.DirectContent.SetLineDash(0);//重置回去，否则后续画线都已最后画线虚实为准
        }
    }
}
